import storage from '@/libs/storage';

export default {
    state: {
        data: ''
    },
    mutations: {
        updateUser(state, user, dontSetTime) {
            state.data = user;
            if (!dontSetTime) {
                localStorage.setItem('LoginTime', new Date().getTime())
            }
            storage.updateUser(user).then(() => {})
        },
        logout(state) {
            // Cookies.remove('user');
            // Cookies.remove('password');
            // Cookies.remove('access');
            localStorage.removeItem('LoginTime')
            localStorage.removeItem('access')

            state.data = '';
            //    恢复默认样式
            let themeLink = document.querySelector('link[name="theme"]');
            themeLink.setAttribute('href', '');
            // 清空打开的页面等数据，但是保存主题数据
            let theme = '';
            if (localStorage.theme) {
                theme = localStorage.theme;
            }
            localStorage.clear();
            if (theme) {
                localStorage.theme = theme;
            }
            storage.clearUser().then(() => {})
        },
        getUser(state) {
            if (new Date().getTime() - Number(localStorage.getItem('LoginTime')) > 7 * 24 * 60 * 60 * 1000) {
                localStorage.removeItem('LoginTime')
                localStorage.removeItem('access')
                state.data = '';
                storage.clearUser();
            } else {
                storage.getUser().then((user) => {
                    state.data = user;
                    console.log(user);
                })
            }
        }
    }
}